<template>
  <a-modal title='订单商品' v-model='visible' v-if='visible' width='800px' height='500px' @cancel='handleCancel' @ok='handleAddDrug'>
    <f-form ref='orderDrugsForm' :label-width='4' :action='api.bod' v-if='visible' :form-key='this.formItem.orderDetailsId'>
      <!--@click='openDrugsModal'-->
      <f-form-item label='商品名称' placeholder='请选择商品' name='drugsName' @click='openDrugsModal'  :readOnly='true' :required='true' :value.sync='formItem.drugsName' :trigger-change='true'>
        <a-tooltip slot='addonBefore' placement='top' title='点击选择商品'>
          <a-icon type='experiment' style='cursor:pointer' />
        </a-tooltip>
      </f-form-item>
      <f-form-item label='商品批号' placeholder='请选择批号' v-if='formItem.drugNo' name='drugLotNo' @click='openDrugsLotModal'  :required='true' :readOnly='true' :value.sync='formItem.drugLotNo' :trigger-change='true'>
        <a-tooltip slot='addonBefore' placement='top' title='点击选择批号' @click='openDrugsLotModal' >
          <a-icon type='barcode' style='cursor:pointer' />
        </a-tooltip>
        <a-tooltip slot='suffix' placement='topRight' title='删除批号'>
          <a-icon type='delete' @click='deleteDrugsLot' />
        </a-tooltip>
      </f-form-item>
      <!--:max='formItem.surplusNum'-->
      <f-form-item label='销售数量' type='number' v-if='formItem.drugNo' data-type='number' name='saleNum'  :min='1' :required='true' :value.sync='formItem.saleNum' />
      <a-row v-if='formItem.drugNo' style='margin-left: 36px;'>
        <a-col :span='18'>
          <f-form-item label='销售价格' type='number' :precision='4' data-type='number' name='salePrice' :required='true' :value.sync='formItem.salePrice' />
        </a-col>
        <a-col :span='3' style='margin-left: 20px;margin-top: 5px'>
          <a-button type='primary' icon='search' @click='openHistoryDrugModal'>
            历史价格查询
          </a-button>
        </a-col>
      </a-row>
      <f-form-item label='销售金额' type='number' v-if='formItem.saleAmount>0' data-type='number' name='saleAmount' disabled :value.sync='formItem.saleAmount' />
      <f-form-item label='票折金额' type='number' data-type='number' name='discountPrice' :min='0' :value.sync='formItem.discountPrice' />
      <!--      <f-form-item label='资保金账户' placeholder='请选择抵扣金额' name='customBond' @click='openSaleManBalanceModal' data-type='number' :precision='4' :readOnly='true' :required='true' :value.sync='formItem.customBond' :trigger-change='true' v-if='takeAmountType==2 && this.sendType==3'>
              <a-tooltip slot='addonBefore' placement='top' title='点击图标选择'>
                <a-icon type='transaction' style='cursor:pointer' @click='openSaleManBalanceModal' />
              </a-tooltip>
            </f-form-item>
            <f-form-item label='预收款账户' placeholder='请选择抵扣金额' name='returnAmount' @click='openCustomBalanceModal' data-type='number' :precision='4' :readOnly='true' :required='true' :value.sync='formItem.returnAmount' :trigger-change='true' v-if='takeAmountType==2 && this.sendType==2 && formItem.saleNum>0'>
              <a-tooltip slot='addonBefore' placement='top' title='点击图标选择'>
                <a-icon type='transaction' style='cursor:pointer' @click='openCustomBalanceModal' />
              </a-tooltip>
            </f-form-item>-->
    </f-form>
    <select-hy-drugs-modal ref='drugsModal' @drugsSelectChange='drugsLotSelectChange'></select-hy-drugs-modal>
    <select-hy-drugs-lot-modal ref='drugsLotModal' @drugsLotSelectChange='drugsLotSelectChange'></select-hy-drugs-lot-modal>
    <select-custom-balance-modal ref='customBalanceModal' @onSelectChange='onSelectCustomBalanceChange'></select-custom-balance-modal>
    <select-saleman-balance-modal ref='saleManBalanceModal' @onSelectChange='onSelectSaleManBalanceChange'></select-saleman-balance-modal>
    <select-history-drugs-modal ref='historyDrugs'></select-history-drugs-modal>
  </a-modal>
</template>
<script>
import api from "../js/api"
import SelectHyDrugsModal from "../components/select_hy_drugs_modal"
import SelectHyDrugsLotModal from "../components/select_hy_drugslot_modal"
import SelectCustomBalanceModal from "../components/select_custom_balance_modal"
import SelectSalemanBalanceModal from "../components/select_saleman_balance_modal"
import SelectHistoryDrugsModal from "../components/select_history_drugs_modal"

export default {
  name: "OrderDrugModal",
  props: {},
  data() {
    return {
      refs: this.$refs,
      api: Object.assign(api, this.api),
      loading: false,
      visible: false,
      isAdd: true,
      customId: null,
      customCode: null,
      hyDeptNo: null,
      saleManId: null,
      surplusNum: 0,
      deptType:"ZY",
      takeAmountType: 1,
      sendType: 0,
      formItem: this.getFormItem(),
      queryParam: {}
    }
  },
  watch: {
    "formItem.salePrice": function(value) {
      if (this.formItem.saleNum) {
        this.formItem.saleAmount = parseFloat(value * this.formItem.saleNum).toFixed(2)
      }

    },
    "formItem.saleNum": function(value) {
      if (this.formItem.salePrice) {
        this.formItem.saleAmount = parseFloat(value * this.formItem.salePrice).toFixed(2)
      }
    }
  },
  methods: {
    getFormItem() {
      return {
        takeAmountType: 1,
        customBalanceId: null,
        orderDetailsId: "",
        drugsName: "",
        supplierId: "",
        drugsId: "",
        customBond: 0,
        policyDrugId: "",
        drugLotNo: "",
        drugLotId: "",
        saleAmount: 0
      }
    },
    open(params, record) {
      this.formItem.takeAmountType = params.takeAmountType
      this.takeAmountType = params.takeAmountType
      this.sendType = params.sendType
      this.customId = params.customId
      this.customCode = params.customCode
      this.hyDeptNo = params.hyDeptNo
      this.deptType = params.deptType || "ZY"
      this.saleManId = params.saleManId
      if (record) {
        this.isAdd = false
        this.formItem = record
        if (!record.saleManBalanceId) {
          this.formItem.customBond = 0
        }
        if (!record.customBalanceId) {
          this.formItem.returnAmount = 0
        }
      } else {
        this.isAdd = true
        this.formItem = this.getFormItem()
      }
      this.visible = true
    },
    close() {
      this.$emit("close")
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleSubmit() {
      this.close()
    },
    //商品选择框
    openDrugsModal() {
      this.$refs["drugsModal"].open(this.hyDeptNo,this.deptType)
    },
    //清空规格
    deleteDrugsLot() {
      this.$refs["orderDrugsForm"].getForm().setFieldsValue({ "drugLotNo": "" })
      this.formItem.drugLotId = ""
    },
    //规格选择框
    openDrugsLotModal() {
      this.$refs["drugsLotModal"].open(this.formItem.drugNo, this.formItem.drugsName, this.hyDeptNo)
    },
    //打开资保金余额选择
    openSaleManBalanceModal() {
      this.$refs["saleManBalanceModal"].open({
        saleManId: this.saleManId
      })
    },
    //客户历史售价信息
    openHistoryDrugModal() {
      this.$refs["historyDrugs"].open(
        this.formItem.drugNo,
        this.customCode
      )
    },
    //客户货款余额
    openCustomBalanceModal() {
      this.$refs["customBalanceModal"].open({
        customId: this.customId,
        customCode: this.customCode,
        amountType: "YfkAmount"
      })
    },
    handleAddDrug() {
      let that = this
      this.$refs["orderDrugsForm"].getForm().validateFields((valid, values) => {
        if (!valid) {
          let costPrice = this.formItem.costPrice
          console.info("costPrice",costPrice)
          let salePrice = this.formItem.salePrice
          if (costPrice > salePrice) {
            this.$confirm({
              title: "温馨提示!",
              content: "当前销售价格低于成本价,是否继续",
              okText: "是",
              okType: "danger",
              cancelText: "否",
              onOk() {
                let drug = Object.assign({}, that.formItem)
                that.$emit("onSubmit", drug, that.isAdd)
                that.visible = false
              }
            })
          }else{
            let drug = Object.assign({}, this.formItem)
            this.$emit("onSubmit", drug, this.isAdd)
            this.visible = false
          }

        }
      })
    },
    //商品选择回调
    drugsSelectChange(selectedRow) {
      this.$refs["orderDrugsForm"].getForm().setFieldsValue({ "drugsName": selectedRow.drugsName })
      this.formItem.drugsName = selectedRow.drugsName
      this.formItem.drugNo = selectedRow.drugNo
      this.formItem.drugsSpec = selectedRow.drugsSpec
      this.formItem.drugJr = selectedRow.drugJr
      this.formItem.producersName = selectedRow.producersName
      this.formItem.drugType = selectedRow.drugType
      this.$forceUpdate()
    },
    //批号选择回调
    drugsLotSelectChange(selectedRow, drugSelectRow) {
      if (drugSelectRow) {
        this.drugsSelectChange(drugSelectRow)
      }
      this.$refs["orderDrugsForm"].getForm().setFieldsValue({ "drugLotNo": selectedRow.drugLotNo })
      this.formItem.drugLotId = selectedRow.drugLotId
      this.formItem.costPrice = selectedRow.costPrice
      this.formItem.drugLotNo = selectedRow.drugLotNo
      this.formItem.surplusNum = selectedRow.canUseNum
      this.formItem.storeCode = selectedRow.storeCode
      this.formItem.storeName = selectedRow.storeName
      this.formItem.expirationDate = selectedRow.expirationDate
      this.formItem.generationDate = selectedRow.generationDate
      this.$forceUpdate()
    },
    /**
     * 货款余额记录择回调
     */
    onSelectCustomBalanceChange(selectedRowKeys, selectedRows) {
      this.$refs["orderDrugsForm"].getForm().setFieldsValue({ "returnAmount": selectedRows[0].amount })
      this.formItem.returnAmount = selectedRows[0].amount
      this.formItem.customBalanceId = selectedRowKeys[0]
      this.$forceUpdate()
    },
    /**
     * 资保金余额记录择回调
     */
    onSelectSaleManBalanceChange(selectedRowKeys, selectedRows) {
      this.$refs["orderDrugsForm"].getForm().setFieldsValue({ "customBond": selectedRows[0].amount })
      this.formItem.customBond = selectedRows[0].amount
      this.formItem.saleManBalanceId = selectedRowKeys[0]
      this.$forceUpdate()
    }
  },
  created() {
  },
  components: { SelectHistoryDrugsModal, SelectHyDrugsLotModal, SelectHyDrugsModal, SelectCustomBalanceModal, SelectSalemanBalanceModal }
}
</script>

<style scoped lang='less' rel='stylesheet/less'>

</style>
